package com.shawn.baseappbygroovydsl.ui.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.chad.library.adapter4.BaseMultiItemAdapter
import com.choryan.quan.videowzproject.extension.ExtensionView.hide
import com.choryan.quan.videowzproject.extension.ExtensionView.show
import com.shawn.baseappbygroovydsl.R

import eee.drama.idea_chat.bean.MessageChatBean
import eee.drama.idea_chat.databinding.ItemChatLeftBinding
import eee.drama.idea_chat.databinding.ItemChatLeftRedStyle0Binding
import eee.drama.idea_chat.databinding.ItemChatLeftRedStyle1Binding
import eee.drama.idea_chat.databinding.ItemChatLeftRedStyle2Binding
import eee.drama.idea_chat.databinding.ItemChatRightBinding
import kotlin.properties.Delegates


class ChatContentListAdapter(listener: OnItemChildClickListener<MessageChatBean>) : BaseMultiItemAdapter<MessageChatBean>() {

    class ItemVHUser(val viewBinding: ItemChatRightBinding) : RecyclerView.ViewHolder(viewBinding.root)
    class ItemVHAi(val viewBinding: ItemChatLeftBinding) : RecyclerView.ViewHolder(viewBinding.root)
    class ItemVHRedStyle0(val viewBinding: ItemChatLeftRedStyle0Binding) : RecyclerView.ViewHolder(viewBinding.root)
    class ItemVHRedStyle1(val viewBinding: ItemChatLeftRedStyle1Binding) : RecyclerView.ViewHolder(viewBinding.root)
    class ItemVHRedStyle2(val viewBinding: ItemChatLeftRedStyle2Binding) : RecyclerView.ViewHolder(viewBinding.root)
    init {
        addItemType(ITEM_TYPE_USER, object : OnMultiItemAdapterListener<MessageChatBean, ItemVHUser> {
            override fun onCreate(context: Context, parent: ViewGroup, viewType: Int): ItemVHUser {
                val viewBinding = ItemChatRightBinding.inflate(LayoutInflater.from(context), parent, false)
                return ItemVHUser(viewBinding)
            }

            override fun onBind(holder: ItemVHUser, position: Int, item: MessageChatBean?) {
                item?.let {
                    loadUserIMessage(holder, item)
                }
            }
        }).addItemType(ITEM_TYPE_AI, object : OnMultiItemAdapterListener<MessageChatBean, ItemVHAi> {
            override fun onCreate(context: Context, parent: ViewGroup, viewType: Int): ItemVHAi {
                val viewBinding = ItemChatLeftBinding.inflate(LayoutInflater.from(context), parent, false)
                return ItemVHAi(viewBinding)
            }

            override fun onBind(holder: ItemVHAi, position: Int, item: MessageChatBean?) {
                item?.let {
                    loadAIMessage(holder, item)
                }
            }
        }).addItemType(ITEM_TYPE_RED_STYLE_0, object : OnMultiItemAdapterListener<MessageChatBean, ItemVHRedStyle0> {
            override fun onCreate(context: Context, parent: ViewGroup, viewType: Int): ItemVHRedStyle0 {
                val viewBinding = ItemChatLeftRedStyle0Binding.inflate(LayoutInflater.from(context), parent, false)
                return ItemVHRedStyle0(viewBinding)
            }

            override fun onBind(holder: ItemVHRedStyle0, position: Int, item: MessageChatBean?) {
                item?.let {
                    loadHbByStyle0(holder, item)
                }
            }
        }).addItemType(ITEM_TYPE_RED_STYLE_1, object : OnMultiItemAdapterListener<MessageChatBean, ItemVHRedStyle1> { // 类型 1
            override fun onCreate(context: Context, parent: ViewGroup, viewType: Int): ItemVHRedStyle1 {
                val viewBinding = ItemChatLeftRedStyle1Binding.inflate(LayoutInflater.from(context), parent, false)
                return ItemVHRedStyle1(viewBinding)
            }

            override fun onBind(holder: ItemVHRedStyle1, position: Int, item: MessageChatBean?) {
                item?.let {
                    loadHbByStyle1(holder, item)
                }
            }
        }).addItemType(ITEM_TYPE_RED_STYLE_2, object : OnMultiItemAdapterListener<MessageChatBean, ItemVHRedStyle2> { // 类型 1
            override fun onCreate(context: Context, parent: ViewGroup, viewType: Int): ItemVHRedStyle2 {
                val viewBinding = ItemChatLeftRedStyle2Binding.inflate(LayoutInflater.from(context), parent, false)
                return ItemVHRedStyle2(viewBinding)
            }

            override fun onBind(holder: ItemVHRedStyle2, position: Int, item: MessageChatBean?) {
                item?.let {
                    loadHbByStyle2(holder, item)
                }
            }
        })

        addOnItemChildClickListener(eee.drama.idea_chat.R.id.cl_hongbao,listener)
    }

    var message by Delegates.observable(0, onChange = { _, old, new ->
        if (old != new) {
            notifyDataSetChanged()
        }
    })


    override fun getItemId(position: Int): Long {
        return position.toLong()
    }

    private fun loadHbByStyle0(holder: ItemVHRedStyle0, item: MessageChatBean) {
        val img = holder.viewBinding.sivIcon

        val ivHongBao = holder.viewBinding.layoutRed.hongbao
        val tvMsg = holder.viewBinding.layoutRed.tvMsg
        val zuoJianTou = holder.viewBinding.layoutRed.zuojiantou
        val ivBg = holder.viewBinding.layoutRed.ivRedBgOff
        val tvName = holder.viewBinding.tvName
        tvName.text = item.name
        if (item.isClick) {
            zuoJianTou.setImageResource(eee.drama.idea_chat.R.drawable.speak_red_left_jiantou_off)
            ivBg.visibility = View.VISIBLE
            ivHongBao.setImageResource(eee.drama.idea_chat.R.drawable.speak_red_hongbao_open)
            tvMsg.text = "已领取"
        } else {
            zuoJianTou.setImageResource(eee.drama.idea_chat.R.drawable.speak_red_left_jiantou)
            ivBg.visibility = View.GONE
            tvMsg.text = "恭喜发财，大吉大利"
            ivHongBao.setImageResource(eee.drama.idea_chat.R.drawable.speak_red_hongbao)
        }

        Glide.with(context)
            .load(item.icon)
            .circleCrop()
            .into(img)
    }

    private fun loadHbByStyle1(holder: ItemVHRedStyle1, item: MessageChatBean) {
        val img = holder.viewBinding.sivIcon

        val ivHongBao = holder.viewBinding.layoutRed.hongbao
        val tvMsg = holder.viewBinding.layoutRed.tvMsg
        val zuoJianTou = holder.viewBinding.layoutRed.zuojiantou
        val ivBg = holder.viewBinding.layoutRed.ivRedBgOff
        val tvName = holder.viewBinding.tvName
        val decorate1 = holder.viewBinding.ivDecorate1
        val decorate2 = holder.viewBinding.ivDecorate2
        tvName.text = item.name
        if (item.isClick) {
            zuoJianTou.setImageResource(eee.drama.idea_chat.R.drawable.speak_red_left_jiantou_off)
            ivBg.visibility = View.VISIBLE
            ivHongBao.setImageResource(eee.drama.idea_chat.R.drawable.speak_red_hongbao_open)
            tvMsg.text = "恭喜发财，大吉大利"
            decorate1.hide()
            decorate2.hide()
        } else {
            zuoJianTou.setImageResource(eee.drama.idea_chat.R.drawable.speak_red_left_jiantou)
            ivBg.visibility = View.GONE
            tvMsg.text = "恭喜发财，大吉大利"
            ivHongBao.setImageResource(eee.drama.idea_chat.R.drawable.speak_red_hongbao)
            decorate1.show()
            decorate2.show()
        }

        Glide.with(context)
            .load(item.icon)
            .circleCrop()
            .into(img)
    }

    private fun loadHbByStyle2(holder: ItemVHRedStyle2, item: MessageChatBean) {
        val img = holder.viewBinding.sivIcon

        val ivHongBao = holder.viewBinding.layoutRed.hongbao
        val tvMsg = holder.viewBinding.layoutRed.tvMsg
        val zuoJianTou = holder.viewBinding.layoutRed.zuojiantou
        val ivBg = holder.viewBinding.layoutRed.ivRedBgOff
        val tvName = holder.viewBinding.tvName
        val decorate1 = holder.viewBinding.ivDecorate1
        val decorate2 = holder.viewBinding.ivDecorate2
        tvName.text = item.name
        if (item.isClick) {
            zuoJianTou.setImageResource(eee.drama.idea_chat.R.drawable.speak_red_left_jiantou_off)
            ivBg.visibility = View.VISIBLE
            ivHongBao.setImageResource(eee.drama.idea_chat.R.drawable.speak_red_hongbao_open)
            tvMsg.text = "恭喜发财，大吉大利"
            decorate1.hide()
            decorate2.hide()
        } else {
            zuoJianTou.setImageResource(eee.drama.idea_chat.R.drawable.speak_red_left_jiantou)
            ivBg.visibility = View.GONE
            tvMsg.text = "恭喜发财，大吉大利"
            ivHongBao.setImageResource(eee.drama.idea_chat.R.drawable.speak_red_hongbao)
            decorate1.show()
            decorate2.show()
        }

        Glide.with(context)
            .load(item.icon)
            .circleCrop()
            .into(img)
    }

    private fun loadUserIMessage(holder: ItemVHUser, item: MessageChatBean) {
        holder.viewBinding.tvContent.text = item.msg
        Glide.with(context)
            .load(R.drawable.ic_menu_camera)
            .circleCrop()
            .into(holder.viewBinding.sivIcon)
    }

    private fun loadAIMessage(holder: ItemVHAi, item: MessageChatBean) {
        val img = holder.viewBinding.sivIcon
        val tx = holder.viewBinding.tvContent
        val name = holder.viewBinding.tvName
        Glide.with(context)
            .load(item.icon)
            .circleCrop()
            .into(img)
        name.text = item.name
        tx.text = item.msg
    }

    companion object {
        const val ITEM_TYPE_AI = 0
        const val ITEM_TYPE_USER = 1
        const val ITEM_TYPE_RED_STYLE_0 = 100
        const val ITEM_TYPE_RED_STYLE_1 = 101
        const val ITEM_TYPE_RED_STYLE_2 = 102
    }
}